﻿Public Class DperfilUsuario

    Public Shared Function SeleccionarTodo(ByVal descEmpresa As String) As List(Of EperfilUsuario)
        Dim perfils As List(Of EperfilUsuario) = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_perfilUsuario_sel", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            perfils = New List(Of EperfilUsuario)
                            Dim perfil As EperfilUsuario = Nothing
                            Do While dr.Read
                                perfil = New EperfilUsuario( _
                                    dr.GetInt32(dr.GetOrdinal("idPerfilUsuario")), _
                                    dr.GetString(dr.GetOrdinal("descPerfilUsuario")), _
                                    dr.GetString(dr.GetOrdinal("estado")))
                                perfils.Add(perfil)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return perfils
    End Function
    Public Shared Function SeleccionarPerfil(ByVal descEmpresa As String, ByVal idPerfil As Int32) As EperfilUsuario
        Dim perfil As EperfilUsuario = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_perfilxUsuario_sel", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idPerfil", SqlDbType.Int).Value = idPerfil
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            Do While dr.Read
                                perfil = New EperfilUsuario
                                perfil = New EperfilUsuario( _
                                    dr.GetInt32(dr.GetOrdinal("idPerfilUsuario")), _
                                    dr.GetString(dr.GetOrdinal("descPerfilUsuario")), _
                                    dr.GetString(dr.GetOrdinal("estado")))
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return perfil
    End Function

    Public Shared Function Añadir(ByVal descEmpresa As String, ByVal pPerfiles As EperfilUsuario) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_perfilUsuario_add", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@descPerfilUsuario", SqlDbType.VarChar, 55).Value = pPerfiles.descPerfilUsuario
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pPerfiles.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Editar(ByVal descEmpresa As String, ByVal pPerfiles As EperfilUsuario) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_perfilUsuario_edit", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idPerfilUsuario", SqlDbType.Int).Value = pPerfiles.idPerfilUsuario
                cmd.Parameters.Add("@descPerfilUsuario", SqlDbType.VarChar, 55).Value = pPerfiles.descPerfilUsuario
                cmd.Parameters.Add("@estado", SqlDbType.Char, 1).Value = pPerfiles.estado
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pPerfiles.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Eliminar(ByVal descEmpresa As String, ByVal pPerfiles As EperfilUsuario) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_perfilUsuario_del", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idPerfilUsuario", SqlDbType.Int).Value = pPerfiles.idPerfilUsuario
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pPerfiles.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function

End Class
